<script setup lang="ts">
import axios from 'axios';
import { ref, watch } from 'vue';
import { Article, ArticleResData } from '../types/data';

// 1. 定义属性接收频道id
const props = defineProps<{
  channelId: number
}>()

// 2. 定义变量存储接口数据
const list = ref<Article[]>([])

// 3. 监听频道id的变化, 一旦变化重新拉取数据
watch(() => props.channelId, async () => {
  const res = await axios.get<ArticleResData>('http://geek.itheima.net/v1_0/articles', {
    params: {
      channel_id: props.channelId,
      timestamp: Date.now()
    }
  })
  list.value = res.data.data.results
}, {
  // 开始立马执行1次
  immediate: true
})

</script>

<template>
  <div class="article-list">
    <div class="article-item" v-for="item in list" :key="item.art_id">
      <p class="title">{{ item.title }}</p>
      <img v-for="(pic, index) in item.cover.images" :key="index" class="img" :src="pic" alt="">
      <div class="info">
        <span>{{ item.aut_name }}</span>
        <span>{{ item.comm_count }}评论</span>
        <span>{{ item.pubdate }}</span>
      </div>
    </div>
  </div>
</template>
